/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Class.RepeatPaginator;
import DAO.AccountDAO;
import DAO.DriversDAO;
import DAO.PaymentDAO;
import DAO.RequestDAO;
import Models.Account;
import Models.Drivers;
import Models.Payment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cham to tet
 */
@ManagedBean
@RequestScoped
public class DriversController {

    String CitySearch, Keyword, FindWhere;
    List<Drivers> listDr = new ArrayList<Drivers>();
    List listCity = new ArrayList();
    RepeatPaginator paginator;

    public RepeatPaginator getPaginator() {
        return paginator;
    }

    public void searchByCity() {
        try {
            this.paginator = new RepeatPaginator(DriversDAO.getDriversByKeyword(CitySearch, "City"));
        } catch (Exception ex) {
            Logger.getLogger(DriversController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void advanceSearch() {
        try {
            this.paginator = new RepeatPaginator(DriversDAO.getDriversByKeyword(Keyword, FindWhere));
        } catch (Exception ex) {
            Logger.getLogger(DriversController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Creates a new instance of DriversController
     */
    public DriversController() {
        try {
            this.paginator = new RepeatPaginator(DriversDAO.getAllDriver());
        } catch (Exception ex) {
            Logger.getLogger(DriversController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getCitySearch() {
        return CitySearch;
    }

    public String getFindWhere() {
        return FindWhere;
    }

    public void setFindWhere(String FindWhere) {
        this.FindWhere = FindWhere;
    }

    public String getKeyword() {
        return Keyword;
    }

    public void setKeyword(String Keyword) {
        this.Keyword = Keyword;
    }

    public void setCitySearch(String CitySearch) {
        this.CitySearch = CitySearch;
    }

    public List<Drivers> getListDr() throws Exception {
        listDr = DriversDAO.getAllDriver();
        return listDr;
    }

    public List getListCity() throws Exception {
        listCity = DriversDAO.getAllCity();
        return listCity;
    }

    public void processRegister() {
        Drivers driver = (Drivers) (FacesContext.getCurrentInstance().getExternalContext()).getRequestMap().get("driverBean");
        Account acc = (Account) (FacesContext.getCurrentInstance().getExternalContext()).getRequestMap().get("accountBean");
        Payment payment = (Payment) (FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("paymentBean"));

        new DriversDAO().addDrivers(driver);
        acc.setCompanyID(0);
        acc.setDriversID(new DriversDAO().getCurrentID());
        new AccountDAO().addAccount(acc);

        payment.setCompanyID(0);
        payment.setDriversID(new DriversDAO().getCurrentID());
        payment.setAdvertiseID(0);
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();
        HttpSession session = (HttpSession) e.getSession(true);
        int money;
        if (payment.getExtensionType().equalsIgnoreCase("Monthly")) {
            money = payment.getAmount() * 10;
        } else {
            money = payment.getAmount() * 25;
        }
        payment.setMoney(money);
        session.setAttribute("Money", money);
        new PaymentDAO().addPayment(payment);
        try {
            if (payment.getPaymentType().equals("Cash")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("PayByCash.xhtml");
            } else {
                FacesContext.getCurrentInstance().getExternalContext().redirect("PayByBank.xhtml");
            }
        } catch (IOException ex) {
            Logger.getLogger(DriversController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void selectedID(int selectedID) {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();

        HttpSession session = (HttpSession) e.getSession(true);
        session.setAttribute("selectedID", selectedID);
    }

    public void deleteDriver() throws Exception {
        ExternalContext e = FacesContext.getCurrentInstance().getExternalContext();

        HttpSession session = (HttpSession) e.getSession(true);
        int id = Integer.parseInt(session.getAttribute("selectedID").toString());
        DriversDAO.deleteDriver(id);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information: ", "Driver has been deleted!!"));
    }
}
